package cn.wizzer.app.web.modules.controllers.platform.user;

import cn.wizzer.app.user.modules.models.Goods;
import cn.wizzer.app.user.modules.services.GoodsService;
import cn.wizzer.app.web.commons.ulog.annotation.SLog;
import cn.wizzer.framework.util.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

@IocBean
@At("/platform/user/goods")
public class GoodsController {
    @Inject
    @Reference
    private GoodsService goodsService;

    @At("/")
    @Ok("beetl:/platform/user/goods/index.html")
    @RequiresPermissions("user.goods.list")
    public void index() {
    }


    /**
     * 商品列表(分页)
     *
     * @param searchKeyword String 商品名
     * @param pageNumber    int
     * @param pageSize      int
     * @return Object
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("user.goods.list")
    public Object data(@Param("language") String language,
                       @Param("searchKeyword") String searchKeyword,
                       @Param("pageNumber") int pageNumber,
                       @Param("pageSize") int pageSize) {
        return Result.success().addData(goodsService.listPage(pageNumber, pageSize, language, searchKeyword, true));
    }


    /**
     * 后台添加商品
     *
     * @param goods Goods
     * @return Object
     */
    @At("/add")
    @Ok("json")
    @RequiresPermissions("user.goods.add")
    @SLog(tag = "添加商品", msg = "商品名称:${args[0].name}")
    public Object add(@Param("..") Goods goods) {
        goodsService.insert(goods);
        return Result.success();
    }


    /**
     * 查询商品
     *
     * @param id String
     * @return Object
     */
    @At("/edit/?")
    @Ok("json")
    @RequiresPermissions("user.goods.edit")
    public Object edit(@Param("id") String id) {
        return Result.success(goodsService.fetch(id));
    }


    /**
     * 后台修改商品
     *
     * @param goods Goods
     * @return Object
     */
    @At("/editDo")
    @Ok("json")
    @RequiresPermissions("user.goods.edit")
    @SLog(tag = "修改商品", msg = "商品名称:${args[0].name}")
    public Object editDo(@Param("..") Goods goods) {
        // 乐观锁
        goodsService.updateWithVersion(goods);
        return Result.success();
    }


    /**
     * 后台启用商品
     *
     * @param id String
     * @return Object
     */
    @At("/enable/?")
    @Ok("json")
    @RequiresPermissions("user.goods.edit")
    @SLog(tag = "上架商品", msg = "商品名:${args[1].getAttribute('name')}")
    public Object enable(@Param("id") String id) {
        goodsService.update(Chain.make("disabled", false), Cnd.where("id", "=", id));
        return Result.success();
    }

    /**
     * 后台禁用商品
     *
     * @param id String
     * @return Object
     */
    @At("/disable/?")
    @Ok("json")
    @RequiresPermissions("user.goods.edit")
    @SLog(tag = "下架商品", msg = "商品名:${args[1].getAttribute('name')}")
    public Object disable(@Param("id") String id) {
        goodsService.update(Chain.make("disabled", true), Cnd.where("id", "=", id));
        return Result.success();
    }


    /**
     * 后台删除商品
     *
     * @param id String
     * @return Object
     */
    @At({"/delete/?", "/delete"})
    @Ok("json")
    @RequiresPermissions("user.goods.delete")
    @SLog(tag = "删除商品", msg = "商品名:${args[1].getAttribute('name')}")
    public Object delete(String id, @Param("ids") String[] ids) {
        if (null != ids && ids.length > 0) {
            goodsService.update(Chain.make("delFlag", true), Cnd.where("id", "in", ids));
        } else {
            goodsService.update(Chain.make("delFlag", true), Cnd.where("id", "=", id));
        }
        return Result.success();
    }

}
